﻿//Author:Michal Sporna
//License: https://operationsneakpeek.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Screens
{
    public static class MapSelector
    {
        #region properties
        public static List<Classes.MapSelectorItem> MapSelectorItems;
        public static bool MapHasBeenChosen;

        private static bool _LockedUnlockedDetermined;
        #endregion


        #region methods



        /// <summary>
        /// draw
        /// </summary>
        public static void Draw()
        {
            //declare 
            Classes.MapSelectorItem msItem;
            Rectangle titleFieldRect;
            Rectangle statisticsFieldRect;
            int y = 90;
            int x = 70;
            StringBuilder titleString;

            try
            {
                Util.Cache.GraphicsDevice.Clear(Color.Black);

                titleString = new StringBuilder("Choose Level");

                //draw map icons
                for (int i = 0; i < World.WorldManager.WorldConfig.Maps.Count; i++)
                {
                    //draw "choose level" string
                    Util.Cache.DrawString(Util.Cache.GetFont("AwardPointsFont").FontFace, titleString.ToString(), new Vector2((Util.Cache.GraphicsDevice.Viewport.Width / 2) - (Util.Cache.GetFont("AwardPointsFont").FontFace.MeasureString("Choose Level").Length()/2),10), 25, Color.White);


                    //new instance
                    msItem = new Classes.MapSelectorItem();

                    //frame
                    Util.Cache.Draw(Util.Cache.GetTexture("MapSelectorFrame").Text2D, new Rectangle(x, y, World.WorldManager.WorldConfig.MapSelectorFrameWidth, World.WorldManager.WorldConfig.MapSelectorFrameHeight), Color.White);
                    //draw an icon of the map within the frame
                    //create rectangle first
                    msItem.Rectangle = new Rectangle(x+2, y+2, World.WorldManager.WorldConfig.MapSelectorFrameWidth - 5, World.WorldManager.WorldConfig.MapSelectorFrameHeight-5);
                   

                  
                    //draw map title above
                    titleFieldRect = new Rectangle(x, y - World.WorldManager.WorldConfig.MapSelectorTitleFrameHeight - 3, World.WorldManager.WorldConfig.MapSelectorFrameWidth, World.WorldManager.WorldConfig.MapSelectorTitleFrameHeight);
                    Util.Cache.Draw(Util.Cache.GetTexture("MessageBox").Text2D, titleFieldRect, Color.White);
                    //draw map's name
                    Util.Cache.DrawString(Util.Cache.GetFont("small").FontFace, World.WorldManager.WorldConfig.Maps[i].Title, new Vector2(titleFieldRect.X +10, titleFieldRect.Y+2), 18, Color.White);


                    //draw statistics
                    //prepare statistics field
                    statisticsFieldRect = new Rectangle(x, y + msItem.Rectangle.Height + 10, World.WorldManager.WorldConfig.MapSelectorFrameWidth, World.WorldManager.WorldConfig.MapSelectorStatisticsFrameHeight);
                    //draw stars field
                    

                   //draw stars
                    switch (World.WorldManager.WorldConfig.Maps[i].Stars)
                    {
                        case 0:
                            {
                                Util.Cache.Draw(Util.Cache.GetTexture("MapStars").Text2D, statisticsFieldRect, Color.White);
                                break;
                            }
                        case 1:
                            {
                                Util.Cache.Draw(Util.Cache.GetTexture("MapStars1").Text2D, statisticsFieldRect, Color.White);
                                break;
                            }
                        case 2:
                            {
                                Util.Cache.Draw(Util.Cache.GetTexture("MapStars2").Text2D, statisticsFieldRect, Color.White);
                                break;
                            }
                        case 3:
                            {
                                Util.Cache.Draw(Util.Cache.GetTexture("MapStars3").Text2D, statisticsFieldRect, Color.White);
                                break;
                            }
                    }

                    //if map unlocked
                    //find prev map
                    #region MAP UNLOCKED/LOCKED?

                    
                   

                    //see if specified location has enough stars to unlock this location
                    if (!_LockedUnlockedDetermined)
                    {
                        
                            //if 
                            //this map's previous map ID is NULL then it's unlocked (no stars needed)
                            if (World.WorldManager.WorldConfig.Maps[i].PreviousMapID != "NULL")
                            {
                                //find previous map and see how many stars it has

                                for (int i1 = 0; i1 < World.WorldManager.WorldConfig.Maps.Count; i1++)
                                {
                                    if(World.WorldManager.WorldConfig.Maps[i1].ID==World.WorldManager.WorldConfig.Maps[i].PreviousMapID)
                                    {
                                        //map found
                                        //check stars
                                        if (World.WorldManager.WorldConfig.Maps[i1].Stars >= World.WorldManager.WorldConfig.Maps[i].StarsNeededToUnlock)
                                        {
                                            //enough stars , unlock this map
                                            World.WorldManager.WorldConfig.Maps[i].MapUnlocked = true;
                                            _LockedUnlockedDetermined = true;
                                            break;
                                        }
                                        else
                                        {
                                            //not enough stars, lock this location
                                            World.WorldManager.WorldConfig.Maps[i].MapUnlocked = false;
                                            _LockedUnlockedDetermined = true;
                                            break;
                                        }
                                    }


                                }


                            }
                            else
                            {
                                //no stars needed, map unlocked
                                World.WorldManager.WorldConfig.Maps[i].MapUnlocked = true;
                                _LockedUnlockedDetermined = true;
                              

                            }

                        
                    }

                    //draw
                    if (_LockedUnlockedDetermined)
                    {
                        if (World.WorldManager.WorldConfig.Maps[i].MapUnlocked)
                        {
                            //draw unlocked
                            Util.Cache.Draw(Util.Cache.GetTexture(World.WorldManager.WorldConfig.Maps[i].ID + "_MapIcon").Text2D, msItem.Rectangle, Color.White);
                        }
                        else
                        {
                            //draw locked
                            Util.Cache.Draw(Util.Cache.GetTexture(World.WorldManager.WorldConfig.Maps[i].ID + "_MapIcon").Text2D, msItem.Rectangle, Color.White);
                        }
                    }

                    #endregion

                    

                    //add map selector item to the list
                    msItem.MapID = World.WorldManager.WorldConfig.Maps[i].ID;

                    //add
                    MapSelectorItems.Add(msItem);

                    //if map fits the screen still, then draw 
                    if (x + 80 + msItem.Rectangle.Width < Util.Cache.GraphicsDevice.Viewport.Width)
                    {
                        //fits
                        x += 80;
                        //y without a change
                    }
                    //move to the next line
                    //else move to the next line
                    else
                    {
                        x = 80;
                        y = msItem.Rectangle.Height + statisticsFieldRect.Height + titleFieldRect.Height + 15;
                    }

                }


                titleString = null;
            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }

        /// <summary>
        /// check if map has been chosen
        /// if yes, set current map
        /// </summary>
        /// <param name="tapPos"></param>
        public static bool IsMapChosen(Vector2 tapPos)
        {
            //declare
            Levels.MapInfo mapInfo;

            try
            {
                for (int i = 0; i < MapSelectorItems.Count; i++)
                {
                    if ((tapPos.X > MapSelectorItems[i].Rectangle.Left && tapPos.X < MapSelectorItems[i].Rectangle.Left + MapSelectorItems[i].Rectangle.Width) && (tapPos.Y > MapSelectorItems[i].Rectangle.Top && tapPos.Y < MapSelectorItems[i].Rectangle.Top + MapSelectorItems[i].Rectangle.Height))
                    {
                        //get map info
                        mapInfo = Levels.LevelManager.GetMapInfoByID(MapSelectorItems[i].MapID);
                        //set is chosen to true
                        if (mapInfo != null)
                        {
                            mapInfo.ChosenMap = true;

                            MapHasBeenChosen = true;

                            #region load chosen map 

                            Util.Cache.SetCurrentScreen(Util.Cache.Screen.LoadMap);

                          
                            #endregion


                            return true;
                        }
                        else
                        {
                            throw new Exception("NO SUCH MAP!");
                        }

                        
                    }

                }


                return false;



            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return false;
            }
        }

        #endregion


    }
}
